package com.jianzhile.jobs.activities.mine;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.jianzhile.jobs.R;
import com.jianzhile.jobs.activities.JobDetailActivity;
import com.jianzhile.jobs.utils.CommonUtil;
import com.jianzhile.jobs.utils.Const;
import com.jianzhile.jobs.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的发布
 */
public class MyPubActivity extends AppCompatActivity {


    public final String TAG = "MyPubActivity";
    @Bind(R.id.content_my_pub_rrecyclerview)
    public RecyclerView rv;

    @Bind(R.id.content_my_pub_refresh_layout)
    public SwipeRefreshLayout sl;

    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_pub);

        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        ab.setElevation(0);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDefaultDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyPubActivity.this.finish();
            }
        });
        ab.setTitle("");



        setupRecyclerView(rv);
        setupSwipeRefreshLayout(sl);


        loadData();
    }


    public void loadData(){
        Map<String, String> params = new HashMap<String, String>();

        HttpUtil.getInstance().requestWithSecurity(this, new Response.Listener<String>() {
            @Override

            public void onResponse(String response) {

                Log.i(TAG, response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }, CommonUtil.getInstance().getParams(params), Const.GET_SIMPLES, false);
    }

    private void setupRecyclerView(RecyclerView recyclerView) {
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);
        // recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST));

    }

    private void setupSwipeRefreshLayout(final SwipeRefreshLayout swipeRefreshLayout) {
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });
        swipeRefreshLayout.setEnabled(true);


    }

    public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ItemViewHolder> {


        @Override
        public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.job_item, parent, false);
                return new ItemViewHolder(view);

        }

        @Override
        public void onBindViewHolder(MyRecyclerViewAdapter.ItemViewHolder holder, int position) {
            holder.container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MyPubActivity.this, JobDetailActivity.class);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return 10;
        }




        public class ItemViewHolder extends RecyclerView.ViewHolder {

            public final View mView;

            @Bind(R.id.job_item_cover_iv)
            public ImageView coverIv;
            @Bind(R.id.job_item_name)
            public TextView nameTv;
            @Bind(R.id.job_item_company_sign)
            public ImageView companyIv;
            @Bind(R.id.job_item_pay)
            public TextView payTv;
            @Bind(R.id.job_item_state)
            public TextView stateTv;
            @Bind(R.id.job_item_time)
            public TextView timeTv;
            @Bind(R.id.job_item_place)
            public TextView placeTv;
            @Bind(R.id.job_item_person)
            public TextView personTv;
            @Bind(R.id.job_item_edit)
            public LinearLayout editLl;
            @Bind(R.id.job_item_qr)
            public LinearLayout qrLl;
            @Bind(R.id.job_item_share)
            public LinearLayout shareLl;
            @Bind(R.id.job_item_copy)
            public LinearLayout copyLl;

            @Bind(R.id.job_item_container)
            public LinearLayout container;

            public ItemViewHolder(View itemView) {
                super(itemView);
                mView = itemView;
                ButterKnife.bind(this, itemView);

            }
        }

    }
}
